package com.nobug.run;

import com.nobug.MainRunSimple;
import com.nobug.MyRobot;
import com.nobug.MyRobot2;
import com.nobug.MyRobot3;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 *
 * 神武钓鱼 全自动钓鱼 升级 挂机的时候 只要不遮挡游戏界面 在等待上钩的时候 可以操作鼠标键盘 拉杆测试修改
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2023-02-02
 */
public class GoFishing3 {

    public static void main(String[] args) {
        runMain(2);
    }

    private static int[] xxyx; //全局定位 定位休闲游戏的坐标
    private static int[] imageLButton; //抛竿定位坐标

    //休闲游戏 到 收取 的偏移量
    private static int xOffset;
    private static int yOffset;


    public static void runMain(int offset) {
        inti(offset);
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        one:
        while (true){
            if(xxyx==null){
                findAllxy2();
            }

            if(imageLButton==null){
                pgdw2();
            }else {
                System.out.println("移动到抛竿位置点击抛竿");
                MyRobot.move(imageLButton);
                sleep(500);
                MyRobot.lButton();
            }
            Random random = new Random();
            int i = random.nextInt(2000)+500;
            sleep(i);
            MyRobot.lButton();
            System.out.println("完成抛竿，等待上钩，启动监测上钩");

            long l = System.currentTimeMillis()+5*60*1000; //五分钟不上钩子 就跳过循环

            //这里修改为直接识别像素点

            MyRobot.sleep(1000);
            int rgb = MyRobot.getRGB(imageLButton);
            System.out.println("rgb:"+rgb);
//            int[] imageLButton1 = MainRunSimple.findImage("sg1.png");
            while (MyRobot.getRGB(imageLButton) == rgb){
                MyRobot.sleep(1000);
                System.out.println("钓鱼监测中，可以移动鼠标，但是请勿覆盖游戏界面，本次已等待"+((System.currentTimeMillis()+5*60*1000-l)/1000)+"秒钟");
                if(System.currentTimeMillis()>l){
                    continue one;
                }
            }
            System.out.println("上钩！双手放开键鼠，请勿操作！！！！！");
            MyRobot.move(imageLButton[0]+50,imageLButton[1]-16);
            sleep(500);

            pullRod2();
            System.out.println("完成拉杆，展示结果，准备收取");

            sleep(1000);
            MyRobot.move(xxyx[0]+xOffset,xxyx[1]+yOffset);
            sleep(500);
            MainRunSimple.findImageLButton("sq.png");
            System.out.println("完成收取");

        }

    }

    /**
     *    初始化偏移量
     *    分辨率 800 X 600 选择 i 值 1
     *    分辨率 1024 X 768 选择 i 值 2
     *    分辨率 1280 X 960 选择 i 值 3

     */

    private static void inti(int i) {
        if(i == 1){
            xOffset = 300;
            yOffset = 141;
        }else if(i == 2){
            xOffset = 414;
            yOffset = 191;
        }else if(i == 3){
            xOffset = 518;
            yOffset = 243;
        }

    }


    //找到游戏全局定位
    private static void findAllxy2() {
        //找到休闲游戏位置
        int flag = 0;
        do {
            if(flag>10){
                System.out.println("定位失败，退出程序");
                return;
            }
            xxyx = MainRunSimple.findImage("simple.png");
            flag++;
        }while (xxyx[0]==0&&xxyx[1]==0);
        xxyx = MyRobot.getMouseXY(0);
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        sleep(5000);
    }


    private static void pgdw2() {
        //找到休闲游戏位置
        int flag = 0;
        do {
            if(flag>10){
                System.out.println("定位失败，退出程序");
                System.exit(0);
            }
            imageLButton = MainRunSimple.findImage("pg1.png");
            flag++;
        }while (imageLButton[0]==0&&imageLButton[1]==0);

        imageLButton = MyRobot.getMouseXY(0);
        System.out.println("成功定位,开始自动钓鱼");
        System.out.println(imageLButton[0]);
        System.out.println(imageLButton[1]);
        System.out.println("成功定位,开始自动钓鱼");
        System.out.println("成功定位,开始自动钓鱼");
        sleep(500);
        MyRobot.lButton();

    }


    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //控制提示按键是否到最左边 或者 最右边
    private volatile static int flag = 0;

    // 来回摆动 区间偏移值 20   70 最佳
    private final static int LX = 21;
    private final static int RX = 69;

    private static Robot  robot = getRobot();

    private static Robot getRobot() {
        Robot robotV = null;
        try {
            robotV = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return robotV;
    }

    private static int X;
    private static int Y;

    private static void pullRod2() {


        X = imageLButton[0];
        Y = imageLButton[1];

        System.out.println("点击叹号开始拉杆");
        MyRobot.lButton();
        sleep(500);


        System.out.println(X);
        System.out.println(Y);
        int rgb = MyRobot2.getRGB(X + 45, Y - 100);

        System.out.println("位置监听 启动》》》》》");
        int index = 1;
        while (true){

            int x = X+LX;
            int y = Y-138;
            int areaColorNum = MyRobot3.getAreaColorNum(x - index-1, y - index, x + index, y + index, -65794);
//            System.out.println("左边数量："+areaColorNum);
            if(areaColorNum >=3){
//                System.out.println("左边到位");
                flag = 1;
                robot.keyPress(KeyEvent.VK_SPACE);
            }

            int xx = X+RX;
            int yy = Y-138;

            int areaColorNum1 = MyRobot3.getAreaColorNum(xx - index, yy - index, xx + index+1, yy + index, -65794);
//                System.out.println("右边数量："+areaColorNum1);
            if(areaColorNum1 >=3){
                flag = 2;
                robot.keyRelease(KeyEvent.VK_SPACE);
            }

            if (rgb != MyRobot2.getRGB(X+45,Y-100)){
                break;
            }

        }
        robot.keyRelease(KeyEvent.VK_SPACE);
        System.out.println("位置监听 结束》》》》》");

    }

    // 老版本拉杆 固定死
    private static void pullRod() {
//        System.out.println("程序启动1秒后启动，正式的去掉");
//        sleep(1000);
        System.out.println("点击叹号开始拉杆");
        MyRobot.lButton();
        sleep(1500);
        MyRobot.keyTime(KeyEvent.VK_SPACE,1300);
        System.out.println("放开");
        sleep(1000);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,800);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,3000);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,2500);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,500);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);
    }
}
